package com.ys.controller.api.outer;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.constant.AppConstant;
import com.ys.entity.sys.TsUserInfo;
import com.ys.util.dh.DhPassUtil;
import com.ys.util.jwt.JwtUtil;
import com.ys.vo.A;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/api/outer/login")
public class ApiOuterLoginController {
	
    @RequestMapping("/login")
	@ResponseBody
	public A login(
			String userId,
			String password) {

		if (userId == null || userId.length() == 0) {
			return A.err("工号不能为空");
		}
		
		if (password == null || password.length() == 0) {
			return A.err("密码不能为空");
		}
		
		// 验证密码
		TsUserInfo tsUserInfo = new TsUserInfo().setUserId(userId).queryFirst();
		String passEncrypt = DhPassUtil.encryptPassword(userId, password, AppConstant.PASSWORD_SALT);
		if (tsUserInfo == null) {
			return A.err("账号不存在");
		} else if (!passEncrypt.equals(tsUserInfo.getUserPassWord())) {
			return A.err("密码有误");
		}
		
		String accessToken = JwtUtil.getAccessToken(tsUserInfo.getUserId());
		
		String userName = tsUserInfo.getUserName();
		String roleName = tsUserInfo.getRoleName();
		String avatar = AppConstant.URL_AVATAR;//头像图片地址
		LoginDto dto = new LoginDto(userId, userName, roleName, avatar, accessToken);
    	return A.suc(dto);
	}
	@Data
	@AllArgsConstructor
	public static class LoginDto {
		private String userId; // 
		private String userName; // 
		private String roleName; // 
		private String avatar; // 
		private String accessToken; // 
	}
}
